{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/yphacker/opt/anaconda3/envs/py36/lib/python3.6/site-packages/ipykernel_launcher.py:3: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
      "  This is separate from the ipykernel package so we can avoid doing imports until\n"
     ]
    }
   ],
   "source": [
    "# 一些常规特征\n",
    "import pandas as pd\n",
    "from tqdm.autonotebook import *\n",
    "from bs4 import BeautifulSoup\n",
    "import re\n",
    "\n",
    "tqdm.pandas()\n",
    "\n",
    "train = pd.read_csv('../data/train.csv')\n",
    "test = pd.read_csv('../data/test.csv')\n",
    "\n",
    "data = pd.concat([train, test], axis=0, sort=False).reset_index(drop=True)\n",
    "data = data.fillna(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def salary_range_min(row):\n",
    "    try:\n",
    "        result = int(str(row['salary_range']).split('-')[0])\n",
    "    except Exception:\n",
    "        result = -1\n",
    "    return result\n",
    "\n",
    "def salary_range_max(row):\n",
    "    try:\n",
    "        result = int(str(row['salary_range']).split('-')[1])\n",
    "    except Exception:\n",
    "        result = -1\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2dc94b0df58546ec90df343739780d03",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "37f4607745ec40c2b22ff1e9468872cf",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "c8040ebf45dd4794af3b7579d099b79b",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=17880.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>salary_min</th>\n",
       "      <th>salary_max</th>\n",
       "      <th>salary_median</th>\n",
       "      <th>salary_range</th>\n",
       "      <th>telecommuting</th>\n",
       "      <th>has_company_logo</th>\n",
       "      <th>has_questions</th>\n",
       "      <th>employment_type</th>\n",
       "      <th>required_experience</th>\n",
       "      <th>required_education</th>\n",
       "      <th>industry</th>\n",
       "      <th>function</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>130000</td>\n",
       "      <td>65000.0</td>\n",
       "      <td>130000</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>73</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   salary_min  salary_max  salary_median  salary_range  telecommuting  \\\n",
       "0          -1           1            0.0             2              0   \n",
       "1          -1           1            0.0             2              0   \n",
       "2           0      130000        65000.0        130000              0   \n",
       "3          -1           1            0.0             2              0   \n",
       "4          -1           1            0.0             2              0   \n",
       "\n",
       "   has_company_logo  has_questions  employment_type  required_experience  \\\n",
       "0                 1              1                0                    0   \n",
       "1                 1              1                2                    6   \n",
       "2                 0              0                2                    0   \n",
       "3                 1              0                2                    1   \n",
       "4                 0              1                4                    5   \n",
       "\n",
       "   required_education  industry  function  \n",
       "0                   0         0         0  \n",
       "1                   0        23        13  \n",
       "2                   2         0         0  \n",
       "3                   2        23        25  \n",
       "4                   8        73        32  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "normal_feature = pd.DataFrame()\n",
    "normal_feature['salary_min'] = data.progress_apply(lambda row:salary_range_min(row), axis=1)\n",
    "normal_feature['salary_max'] = data.progress_apply(lambda row:salary_range_max(row), axis=1)\n",
    "normal_feature['salary_median'] = (normal_feature['salary_max'] + normal_feature['salary_min'])/2\n",
    "normal_feature['salary_range'] = normal_feature['salary_max'] - normal_feature['salary_min']\n",
    "normal_feature['telecommuting'] = list(data['telecommuting'])\n",
    "normal_feature['has_company_logo'] = list(data['has_company_logo'])\n",
    "normal_feature['has_questions'] = list(data['has_questions'])\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "labelencoder = LabelEncoder()\n",
    "normal_feature['employment_type'] = labelencoder.fit_transform(data['employment_type'].astype(str))\n",
    "normal_feature['required_experience'] = labelencoder.fit_transform(data['required_experience'].astype(str))\n",
    "normal_feature['required_education'] = labelencoder.fit_transform(data['required_education'].astype(str))\n",
    "normal_feature['industry'] = labelencoder.fit_transform(data['industry'].astype(str))\n",
    "normal_feature['function'] = labelencoder.fit_transform(data['function'].astype(str))\n",
    "\n",
    "# 对所有字段计算value_counts\n",
    "# def get_value_counts_feature(cols, data):\n",
    "#     value_data = data.groupby(cols).size().reset_index()\n",
    "#     value_data.columns = [cols, cols + '_count']\n",
    "#     data = pd.merge(data, value_data, on=cols, how='left')\n",
    "#     return data[cols + '_count']\n",
    "\n",
    "# for i in data.columns:\n",
    "#     if i == 'fraudulent':\n",
    "#         pass\n",
    "#     else:\n",
    "#         normal_feature[i + '_count'] = get_value_counts_feature(i, data)\n",
    "\n",
    "data['review'] = data.progress_apply(lambda row:str(row['title']) + ' ' + str(row['location']) + ' ' + str(row['company_profile']) + ' ' + \n",
    "                                   str(row['description']) + ' ' + str(row['department']) + ' ' + str(row['requirements']) + ' ' + str(row['benefits']), axis=1)\n",
    "\n",
    "normal_feature.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始计算tf-idf特征\n",
      "计算结束\n",
      "开始进行一些前期处理\n",
      "处理完毕\n",
      "\n",
      "****开始跑 LogisticRegression ****\n",
      "LogisticRegression 处理完毕\n",
      "五折结果 [0.97936, 0.98049, 0.97738, 0.98473, 0.97963]\n",
      "平均结果 0.9803179999999999\n",
      "\n",
      "****开始跑 SGDClassifier ****\n",
      "SGDClassifier 处理完毕\n",
      "五折结果 [0.96777, 0.96748, 0.96861, 0.96974, 0.96747]\n",
      "平均结果 0.968214\n",
      "\n",
      "****开始跑 PassiveAggressiveClassifier ****\n",
      "PassiveAggressiveClassifier 处理完毕\n",
      "五折结果 [0.98954, 0.98925, 0.98784, 0.99293, 0.98953]\n",
      "平均结果 0.989818\n",
      "\n",
      "****开始跑 RidgeClassfiy ****\n",
      "RidgeClassfiy 处理完毕\n",
      "五折结果 [0.98332, 0.98501, 0.98275, 0.98982, 0.98359]\n",
      "平均结果 0.984898\n",
      "\n",
      "****开始跑 LinearSVC ****\n",
      "LinearSVC 处理完毕\n",
      "五折结果 [0.98699, 0.98812, 0.98558, 0.99152, 0.9867]\n",
      "平均结果 0.9877819999999999\n"
     ]
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "\n",
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer\n",
    "import jieba\n",
    "from tqdm import *\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn.linear_model import PassiveAggressiveClassifier\n",
    "from sklearn.linear_model import RidgeClassifier\n",
    "from sklearn.naive_bayes import BernoulliNB\n",
    "from sklearn.naive_bayes import MultinomialNB\n",
    "from sklearn.svm import LinearSVC\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "df_train = data[:len(train)]\n",
    "df_test = data[len(train):]\n",
    "\n",
    "df_train['label'] = df_train['fraudulent'].astype(int)\n",
    "data = pd.concat([df_train, df_test], axis=0, sort=False)\n",
    "data['review'] = data['review'].apply(lambda row:str(row))\n",
    "\n",
    "############################ tf-idf ############################\n",
    "print('开始计算tf-idf特征')\n",
    "tf = TfidfVectorizer(ngram_range=(1, 2), min_df=3, max_df=0.9, use_idf=1, smooth_idf=1, sublinear_tf=1)\n",
    "discuss_tf = tf.fit_transform(data['review']).tocsr()\n",
    "print('计算结束')\n",
    "\n",
    "############################ 切分数据集 ##########################\n",
    "print('开始进行一些前期处理')\n",
    "train_feature = discuss_tf[:len(df_train)]\n",
    "score = df_train['label']\n",
    "test_feature = discuss_tf[len(df_train):]\n",
    "print('处理完毕')\n",
    "\n",
    "######################### 模型函数(返回sklean_stacking结果) ########################\n",
    "def get_sklearn_classfiy_stacking(clf, train_feature, test_feature, score, model_name, class_number, n_folds, train_num, test_num):\n",
    "    print('\\n****开始跑', model_name, '****')\n",
    "    stack_train = np.zeros((train_num, class_number))\n",
    "    stack_test = np.zeros((test_num, class_number))\n",
    "    score_mean = []\n",
    "    skf = StratifiedKFold(n_splits=n_folds, random_state=1017)\n",
    "    tqdm.desc = model_name\n",
    "    for i, (tr, va) in enumerate(skf.split(train_feature, score)):\n",
    "        clf.fit(train_feature[tr], score[tr])\n",
    "        score_va = clf._predict_proba_lr(train_feature[va])\n",
    "        score_te = clf._predict_proba_lr(test_feature)\n",
    "        score_single = accuracy_score(score[va], clf.predict(train_feature[va]))\n",
    "        score_mean.append(np.around(score_single, 5))\n",
    "        stack_train[va] += score_va\n",
    "        stack_test += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "    df_stack = pd.DataFrame()\n",
    "    df_stack['tfidf_' + model_name + '_classfiy_{}'.format(1)] = stack[:, 1]\n",
    "    print(model_name, '处理完毕')\n",
    "    return df_stack, score_mean\n",
    "\n",
    "model_list = [\n",
    "    ['LogisticRegression', LogisticRegression(random_state=1017, C=3)],\n",
    "    ['SGDClassifier', SGDClassifier(random_state=1017, loss='log')],\n",
    "    ['PassiveAggressiveClassifier', PassiveAggressiveClassifier(random_state=1017, C=2)],\n",
    "    ['RidgeClassfiy', RidgeClassifier(random_state=1017)],\n",
    "    ['LinearSVC', LinearSVC(random_state=1017)]\n",
    "]\n",
    "\n",
    "stack_feature = pd.DataFrame()\n",
    "for i in model_list:\n",
    "    stack_result, score_mean = get_sklearn_classfiy_stacking(i[1], train_feature, test_feature, score, i[0], 2, 5, len(df_train), len(df_test))\n",
    "    stack_feature = pd.concat([stack_feature, stack_result], axis=1, sort=False)\n",
    "    print('五折结果', score_mean)\n",
    "    print('平均结果', np.mean(score_mean))\n",
    "normal_feature = pd.concat([stack_feature, normal_feature], axis=1, sort=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始计算tf-idf特征\n",
      "计算结束\n",
      "开始进行一些前期处理\n",
      "处理完毕\n",
      "sgd stacking\n",
      "stack:1/5\n",
      "得分0.03786815014158462\n",
      "stack:2/5\n",
      "得分0.03765604643608347\n",
      "stack:3/5\n",
      "得分0.037879005330643964\n",
      "stack:4/5\n",
      "得分0.037434859111442766\n",
      "stack:5/5\n",
      "得分0.03789069275120299\n",
      "PAC stacking\n",
      "stack:1/5\n",
      "得分0.022159844972041133\n",
      "stack:2/5\n",
      "得分0.02264334208584598\n",
      "stack:3/5\n",
      "得分0.02340489084659103\n",
      "stack:4/5\n",
      "得分0.020502724184960967\n",
      "stack:5/5\n",
      "得分0.022061861536353648\n",
      "MultinomialNB stacking\n",
      "stack:1/5\n",
      "Total e: 1000.0219319380075\n",
      "Total e: 953.6409274063603\n",
      "Total e: 925.8174254985047\n",
      "Total e: 906.302039093391\n",
      "Total e: 891.2576038655386\n",
      "Total e: 878.9295238506995\n",
      "Total e: 868.5842035141704\n",
      "Total e: 859.64153671364\n",
      "Total e: 851.858010150978\n",
      "Total e: 844.8806295717654\n",
      "Total e: 838.6000326228709\n",
      "Total e: 832.918221204479\n",
      "Total e: 827.7796453221669\n",
      "Total e: 823.0853405250981\n",
      "Total e: 818.8139980104461\n",
      "Total e: 814.8671740641404\n",
      "Total e: 811.2148090141906\n",
      "Total e: 807.8313253842401\n",
      "Total e: 804.701592657918\n",
      "Total e: 801.758986213227\n",
      "Total e: 798.9923261733502\n",
      "Total e: 796.3917253531472\n",
      "Total e: 793.9294174409583\n",
      "Total e: 791.5699806022876\n",
      "Total e: 789.3434282602246\n",
      "Total e: 787.2343337401988\n",
      "Total e: 785.22303871007\n",
      "Total e: 783.2897203497068\n",
      "Total e: 781.4439598320466\n",
      "Total e: 779.6716184458691\n",
      "Total e: 777.948411711506\n",
      "Total e: 776.277692073445\n",
      "Total e: 774.6587989829578\n",
      "Total e: 773.0957116797903\n",
      "Total e: 771.5790956778458\n",
      "Total e: 770.1023507702696\n",
      "Total e: 768.6716003228978\n",
      "Total e: 767.2799918226971\n",
      "Total e: 765.9304839379269\n",
      "Total e: 764.6175355598778\n",
      "Total e: 763.3355042377958\n",
      "Total e: 762.0936333973431\n",
      "Total e: 760.8824513059212\n",
      "Total e: 759.7001555957613\n",
      "Total e: 758.5436979336079\n",
      "Total e: 757.4114828212095\n",
      "Total e: 756.3051807008402\n",
      "Total e: 755.223726398833\n",
      "Total e: 754.1708807368329\n",
      "Total e: 753.1415961958292\n",
      "得分0.016534752955048897\n",
      "stack:2/5\n",
      "Total e: 1005.6972203934292\n",
      "Total e: 958.8959482981718\n",
      "Total e: 929.7987611856612\n",
      "Total e: 910.0220870322833\n",
      "Total e: 895.0447530862531\n",
      "Total e: 883.0118816954856\n",
      "Total e: 872.8534514479855\n",
      "Total e: 864.0240104852876\n",
      "Total e: 856.3530133495923\n",
      "Total e: 849.5270440600956\n",
      "Total e: 843.4782962847996\n",
      "Total e: 838.0169640084422\n",
      "Total e: 833.039482286715\n",
      "Total e: 828.5723471416328\n",
      "Total e: 824.5087537566808\n",
      "Total e: 820.7959425537052\n",
      "Total e: 817.3747802988731\n",
      "Total e: 814.1978346658395\n",
      "Total e: 811.2535723905227\n",
      "Total e: 808.5381645222229\n",
      "Total e: 805.9735325123894\n",
      "Total e: 803.5551804879929\n",
      "Total e: 801.2502067967193\n",
      "Total e: 799.0438857529202\n",
      "Total e: 796.948971075519\n",
      "Total e: 794.9590678335329\n",
      "Total e: 793.0470545723766\n",
      "Total e: 791.2115161776501\n",
      "Total e: 789.4568230317584\n",
      "Total e: 787.7540405633607\n",
      "Total e: 786.1131830519861\n",
      "Total e: 784.5332494123094\n",
      "Total e: 783.0046245088168\n",
      "Total e: 781.5313817117988\n",
      "Total e: 780.0978800759003\n",
      "Total e: 778.7099631915324\n",
      "Total e: 777.367292185401\n",
      "Total e: 776.0606490096496\n",
      "Total e: 774.799563715469\n",
      "Total e: 773.5786023197817\n",
      "Total e: 772.3932506698081\n",
      "Total e: 771.2387844661424\n",
      "Total e: 770.1160999068056\n",
      "Total e: 769.0231992268571\n",
      "Total e: 767.954308623537\n",
      "Total e: 766.909123104197\n",
      "Total e: 765.8869527166775\n",
      "Total e: 764.8917728007848\n",
      "Total e: 763.9205299356322\n",
      "Total e: 762.9731816400317\n",
      "得分0.015499274303523403\n",
      "stack:3/5\n",
      "Total e: 1000.0346591126771\n",
      "Total e: 955.2710718552388\n",
      "Total e: 925.5971581047439\n",
      "Total e: 904.883635701676\n",
      "Total e: 888.5625017152864\n",
      "Total e: 875.0965949554178\n",
      "Total e: 863.7356606757448\n",
      "Total e: 854.1831819437255\n",
      "Total e: 845.8638667086037\n",
      "Total e: 838.6144504650044\n",
      "Total e: 832.2195184129304\n",
      "Total e: 826.5304776726783\n",
      "Total e: 821.382915600258\n",
      "Total e: 816.6894924707567\n",
      "Total e: 812.3839147276975\n",
      "Total e: 808.4292622543509\n",
      "Total e: 804.7382876402183\n",
      "Total e: 801.3044620333778\n",
      "Total e: 798.0926910975314\n",
      "Total e: 795.1030073972642\n",
      "Total e: 792.3328717233957\n",
      "Total e: 789.7163250478251\n",
      "Total e: 787.229851524353\n",
      "Total e: 784.871372286932\n",
      "Total e: 782.6261091850565\n",
      "Total e: 780.498998808447\n",
      "Total e: 778.4644667302299\n",
      "Total e: 776.5113487320688\n",
      "Total e: 774.6529695453005\n",
      "Total e: 772.880686290689\n",
      "Total e: 771.1727191971356\n",
      "Total e: 769.5137556448757\n",
      "Total e: 767.9065110987597\n",
      "Total e: 766.3561970221585\n",
      "Total e: 764.8589874334099\n",
      "Total e: 763.4197392231733\n",
      "Total e: 762.0208347613109\n",
      "Total e: 760.6730686602234\n",
      "Total e: 759.3695969206792\n",
      "Total e: 758.0984766160029\n",
      "Total e: 756.8626751332275\n",
      "Total e: 755.6663205841805\n",
      "Total e: 754.5033938024\n",
      "Total e: 753.3699968891998\n",
      "Total e: 752.2647534397258\n",
      "Total e: 751.1846034702974\n",
      "Total e: 750.1335670673791\n",
      "Total e: 749.1121070820407\n",
      "Total e: 748.1174721009158\n",
      "Total e: 747.1440120453296\n",
      "得分0.017569030163385402\n",
      "stack:4/5\n",
      "Total e: 1003.1675661805073\n",
      "Total e: 963.2810215208943\n",
      "Total e: 937.1412661155415\n",
      "Total e: 919.1651542346542\n",
      "Total e: 905.3521985980477\n",
      "Total e: 893.8687395877535\n",
      "Total e: 884.0313762273595\n",
      "Total e: 875.3458984841511\n",
      "Total e: 867.6866562510526\n",
      "Total e: 860.9046163121154\n",
      "Total e: 854.8959920803148\n",
      "Total e: 849.5313404619204\n",
      "Total e: 844.6267248492455\n",
      "Total e: 840.1673668317596\n",
      "Total e: 836.0858000056223\n",
      "Total e: 832.3110088417378\n",
      "Total e: 828.8059885204932\n",
      "Total e: 825.5449934360765\n",
      "Total e: 822.4987268073394\n",
      "Total e: 819.6308873288672\n",
      "Total e: 816.9154758833882\n",
      "Total e: 814.3558424656962\n",
      "Total e: 811.9401634627145\n",
      "Total e: 809.6282323006262\n",
      "Total e: 807.4090940910638\n",
      "Total e: 805.308824434072\n",
      "Total e: 803.2884807207623\n",
      "Total e: 801.3563765886022\n",
      "Total e: 799.5081730564134\n",
      "Total e: 797.7510536891583\n",
      "Total e: 796.072828949359\n",
      "Total e: 794.4675077431837\n",
      "Total e: 792.9197396319369\n",
      "Total e: 791.4238815903492\n",
      "Total e: 789.973351974977\n",
      "Total e: 788.5609517634869\n",
      "Total e: 787.1921707779054\n",
      "Total e: 785.8579065562395\n",
      "Total e: 784.5551570570351\n",
      "Total e: 783.2937303886097\n",
      "Total e: 782.0780049889036\n",
      "Total e: 780.9044716350152\n",
      "Total e: 779.7665972828619\n",
      "Total e: 778.6521414664074\n",
      "Total e: 777.5652472038688\n",
      "Total e: 776.5052142938551\n",
      "Total e: 775.4711134875332\n",
      "Total e: 774.4623259204083\n",
      "Total e: 773.4712910405859\n",
      "Total e: 772.4989408649302\n",
      "得分0.013785820702540665\n",
      "stack:5/5\n",
      "Total e: 1001.5516721447423\n",
      "Total e: 953.535296963463\n",
      "Total e: 924.0737767716139\n",
      "Total e: 904.1388155094741\n",
      "Total e: 889.1855121787141\n",
      "Total e: 876.9014530786188\n",
      "Total e: 866.255864332117\n",
      "Total e: 856.9253536187231\n",
      "Total e: 848.7480901711835\n",
      "Total e: 841.7002325475597\n",
      "Total e: 835.4617368710034\n",
      "Total e: 829.8205650867918\n",
      "Total e: 824.7040965443584\n",
      "Total e: 819.9977366378928\n",
      "Total e: 815.6466326616389\n",
      "Total e: 811.6109264797684\n",
      "Total e: 807.8345415077697\n",
      "Total e: 804.3004225289822\n",
      "Total e: 800.9883094309207\n",
      "Total e: 797.8695037101669\n",
      "Total e: 794.9566471891527\n",
      "Total e: 792.2054455474229\n",
      "Total e: 789.6096054101547\n",
      "Total e: 787.164837018279\n",
      "Total e: 784.8126845708844\n",
      "Total e: 782.5639867800502\n",
      "Total e: 780.424622912142\n",
      "Total e: 778.3783221113885\n",
      "Total e: 776.4331929137657\n",
      "Total e: 774.5616315830571\n",
      "Total e: 772.7507396015048\n",
      "Total e: 771.0020038058349\n",
      "Total e: 769.3108488253577\n",
      "Total e: 767.67622454332\n",
      "Total e: 766.1003337178295\n",
      "Total e: 764.5767495427673\n",
      "Total e: 763.101454175354\n",
      "Total e: 761.6757575479018\n",
      "Total e: 760.2900419905869\n",
      "Total e: 758.9339732283784\n",
      "Total e: 757.6125890449247\n",
      "Total e: 756.3191110320475\n",
      "Total e: 755.0636143338637\n",
      "Total e: 753.8414669111305\n",
      "Total e: 752.6560541258154\n",
      "Total e: 751.5014115351381\n",
      "Total e: 750.3815259519967\n",
      "Total e: 749.2904173741281\n",
      "Total e: 748.2199976260392\n",
      "Total e: 747.1754035601723\n",
      "得分0.01719061254987076\n",
      "RidgeClassfiy stacking\n",
      "stack:1/5\n",
      "得分0.027891094222940078\n",
      "stack:2/5\n",
      "得分0.027187324206301766\n",
      "stack:3/5\n",
      "得分0.028341169371120026\n",
      "stack:4/5\n",
      "得分0.026630769511499314\n",
      "stack:5/5\n",
      "得分0.028273021909215885\n",
      "LinerSVC stacking\n",
      "stack:1/5\n",
      "得分0.021646207631724795\n",
      "stack:2/5\n",
      "得分0.019264520191990864\n",
      "stack:3/5\n",
      "得分0.022609759833055618\n",
      "stack:4/5\n",
      "得分0.01768730249296221\n",
      "stack:5/5\n",
      "得分0.02067747500847793\n",
      "tfidf特征已保存\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import pandas as pd\n",
    "from sklearn.feature_extraction.text import TfidfVectorizer,CountVectorizer\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.linear_model import SGDClassifier,SGDRegressor\n",
    "from sklearn.linear_model import PassiveAggressiveClassifier,PassiveAggressiveRegressor\n",
    "from sklearn.linear_model import Ridge\n",
    "from wordbatch.models import FTRL,FM_FTRL\n",
    "from sklearn.svm import LinearSVR\n",
    "\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "\n",
    "\n",
    "############################ tf-idf ############################\n",
    "print('开始计算tf-idf特征')\n",
    "tf = TfidfVectorizer(min_df=3,  max_features=10000,\n",
    "        strip_accents='unicode', analyzer='word', token_pattern=r'\\w{1,}',\n",
    "        ngram_range=(1, 3), use_idf=1, smooth_idf=1, sublinear_tf=1,\n",
    "        stop_words = 'english')\n",
    "df_train = data[:len(train)]\n",
    "df_test = data[len(train):]\n",
    "train_data = df_train\n",
    "test_data = df_test\n",
    "df_train['label'] = df_train['fraudulent'].astype(int)\n",
    "data = pd.concat([df_train, df_test], axis=0, sort=False)\n",
    "data['review'] = data['review'].apply(lambda row:str(row))\n",
    "discuss_tf=tf.fit_transform(data['review'])\n",
    "train_feat=tf.transform(df_train['review'])\n",
    "test_feat=tf.transform(df_test['review'])\n",
    "print('计算结束')\n",
    "\n",
    "\n",
    "############################ 切分数据集 ##########################\n",
    "print('开始进行一些前期处理')\n",
    "train_feature = train_feat\n",
    "test_feature = test_feat\n",
    "# 五则交叉验证\n",
    "n_folds = 5\n",
    "print('处理完毕')\n",
    "df_stack2 = pd.DataFrame()\n",
    "for label in [\"fake\"]:\n",
    "    score = df_train['label'] \n",
    "    \n",
    "   \n",
    "    ########################### SGD(随机梯度下降) ################################\n",
    "    print('sgd stacking')\n",
    "    stack_train = np.zeros((len(train_data),1))\n",
    "    stack_test = np.zeros((len(test_data),1))\n",
    "    score_va = 0\n",
    "\n",
    "    sk = StratifiedKFold( n_splits=5, random_state=1017)\n",
    "    for i, (tr, va) in enumerate(sk.split(train_feature, score)):\n",
    "        print('stack:%d/%d' % ((i + 1), n_folds))\n",
    "        sgd = SGDRegressor(random_state=1017,)\n",
    "        sgd.fit(train_feature[tr], score[tr])\n",
    "        score_va = sgd.predict(train_feature[va])\n",
    "        score_te = sgd.predict(test_feature)\n",
    "        print('得分' + str(mean_squared_error(score[va], sgd.predict(train_feature[va]))))\n",
    "        stack_train[va,0] = score_va\n",
    "        stack_test[:,0]+= score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "    df_stack2['tfidf_sgd_classfiy_{}'.format(label)] = stack[:,0]\n",
    "\n",
    "\n",
    "    ########################### pac(PassiveAggressiveClassifier) ################################\n",
    "    print('PAC stacking')\n",
    "    stack_train = np.zeros((len(train_data),1))\n",
    "    stack_test = np.zeros((len(test_data),1))\n",
    "    score_va = 0\n",
    "\n",
    "    sk = StratifiedKFold( n_splits=5, random_state=1017)\n",
    "    for i, (tr, va) in enumerate(sk.split(train_feature, score)):\n",
    "        print('stack:%d/%d' % ((i + 1), n_folds))\n",
    "        pac = PassiveAggressiveRegressor(random_state=1017)\n",
    "        pac.fit(train_feature[tr], score[tr])\n",
    "        score_va = pac.predict(train_feature[va])\n",
    "        score_te = pac.predict(test_feature)\n",
    "      \n",
    "        print('得分' + str(mean_squared_error(score[va], pac.predict(train_feature[va]))))\n",
    "        stack_train[va,0] = score_va\n",
    "        stack_test[:,0] += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "\n",
    "    df_stack2['tfidf_pac_classfiy_{}'.format(label)] = stack[:,0]\n",
    "    \n",
    "\n",
    "    ########################### FTRL ################################\n",
    "    print('MultinomialNB stacking')\n",
    "    stack_train = np.zeros((len(train_data),1))\n",
    "    stack_test = np.zeros((len(test_data),1))\n",
    "    score_va = 0\n",
    "\n",
    "    sk = StratifiedKFold( n_splits=5, random_state=1017)\n",
    "    for i, (tr, va) in enumerate(sk.split(train_feature, score)):\n",
    "        print('stack:%d/%d' % ((i + 1), n_folds))\n",
    "        clf = FTRL(alpha=0.01, beta=0.1, L1=0.00001, L2=1.0, D=train_feature.shape[1], iters=50, inv_link=\"identity\", threads=1)\n",
    "        clf.fit(train_feature[tr], score[tr])\n",
    "        score_va = clf.predict(train_feature[va])\n",
    "        score_te = clf.predict(test_feature)\n",
    "      \n",
    "        print('得分' + str(mean_squared_error(score[va], clf.predict(train_feature[va]))))\n",
    "        stack_train[va,0] = score_va\n",
    "        stack_test[:,0] += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "    \n",
    "    df_stack2['tfidf_FTRL_classfiy_{}'.format(label)] = stack[:,0]\n",
    "    \n",
    "    ########################### ridge(RidgeClassfiy) ################################\n",
    "    print('RidgeClassfiy stacking')\n",
    "    stack_train = np.zeros((len(train_data),1))\n",
    "    stack_test = np.zeros((len(test_data),1))\n",
    "    score_va = 0\n",
    "\n",
    "    sk = StratifiedKFold( n_splits=5, random_state=1017)\n",
    "    for i, (tr, va) in enumerate(sk.split(train_feature, score)):\n",
    "        print('stack:%d/%d' % ((i + 1), n_folds))\n",
    "        ridge = Ridge(solver=\"sag\", fit_intercept=True, random_state=42, alpha=30) \n",
    "        ridge.fit(train_feature[tr], score[tr])\n",
    "        score_va = ridge.predict(train_feature[va])\n",
    "        score_te = ridge.predict(test_feature)\n",
    "       \n",
    "        print('得分' + str(mean_squared_error(score[va], ridge.predict(train_feature[va]))))\n",
    "        stack_train[va,0] = score_va\n",
    "        stack_test[:,0] += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "\n",
    "    df_stack2['tfidf_ridge_classfiy_{}'.format(label)] = stack[:,0]\n",
    "    \n",
    "    ############################ Linersvc(LinerSVC) ################################\n",
    "    print('LinerSVC stacking')\n",
    "    stack_train = np.zeros((len(train_data),1))\n",
    "    stack_test = np.zeros((len(test_data),1))\n",
    "    score_va = 0\n",
    "\n",
    "    sk = StratifiedKFold( n_splits=5, random_state=1017)\n",
    "    for i, (tr, va) in enumerate(sk.split(train_feature, score)):\n",
    "        print('stack:%d/%d' % ((i + 1), n_folds))\n",
    "        lsvc = LinearSVR(random_state=1017)\n",
    "        lsvc.fit(train_feature[tr], score[tr])\n",
    "        score_va = lsvc.predict(train_feature[va])\n",
    "        score_te = lsvc.predict(test_feature)\n",
    "       \n",
    "        print('得分' + str(mean_squared_error(score[va], lsvc.predict(train_feature[va]))))\n",
    "        stack_train[va,0] = score_va\n",
    "        stack_test[:,0] += score_te\n",
    "    stack_test /= n_folds\n",
    "    stack = np.vstack([stack_train, stack_test])\n",
    "\n",
    "    df_stack2['tfidf_lsvc_classfiy_{}'.format(label)] = stack[:,0]\n",
    "print('tfidf特征已保存\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "normal_feature = pd.concat([normal_feature, df_stack2], axis=1, sort=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "normal_feature.to_csv('feature/normal_feature.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
